package com.ethereal.petmanagement.util;

import com.ethereal.petmanagement.dto.ResponseDTO;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RequestDispatcherUtil {
    public static void dispatcher(HttpServletRequest req, HttpServletResponse resp, ResponseDTO responseDTO){
        String referer = req.getHeader("Referer");
        req.setAttribute("returnUrl", referer);
        req.setAttribute("errorCode", responseDTO.getCode() );
        req.setAttribute("errorMsg", responseDTO.getMsg());
        try {
            req.getRequestDispatcher("error.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void dispatcher(HttpServletRequest req, HttpServletResponse resp, CodeMsg codeMsg){
        String referer = req.getHeader("Referer");
        req.setAttribute("returnUrl", referer);
        req.setAttribute("errorCode", codeMsg.getCode());
        req.setAttribute("errorMsg", codeMsg.getMsg());
        try {
            req.getRequestDispatcher("error.jsp").forward(req, resp);
        } catch (ServletException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}
